package ssq.utils.port_android;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import ssq.utils.Utilities;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.widget.Toast;

public class UtilsApp extends Application
{
    private static final int   TOAST          = 3;
    protected static final int VIBRATE_LITTLE = 7;
    
    private static UtilsApp    instance;

    public static UtilsApp getApp()
    {
        return instance;
    }

    static protected Handler handler = new Handler()
                                     {
                                         @Override
                                         public void handleMessage(Message msg)
                                         {
                                             switch (msg.what)
                                             {
                                                 case TOAST:
                                                     Toast.makeText(getApp(), (String) msg.obj, Toast.LENGTH_LONG).show();
                                                     break;
                                                 
                                                 case VIBRATE_LITTLE:
                                                     vibrateLittle();
                                                     break;
                                                 default:
                                                     break;
                                             }
                                             super.handleMessage(msg);
                                         }
                                     };

    static protected void vibrateOK()
    {
        Vibrator vibrator = (Vibrator) getApp().getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(new long[] { 0, 100, 100, 100, 100, 50, 50, 50, 50, 50, 50 }, -1);
    }

    static protected void vibrateLittle()
    {
        Vibrator vibrator = (Vibrator) getApp().getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(new long[] { 0, 80 }, -1);
    }

    static protected void vibrateOh()
    {
        Vibrator vibrator = (Vibrator) getApp().getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(new long[] { 0, 100, 100, 100, 100, 100 }, -1);
    }

    protected static String getSimpleTime(long time)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        return simpleFormat.format(calendar.getTime());
    }

    protected static String getSimpleDate(long time)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy.MM.dd");
        return simpleFormat.format(calendar.getTime());
    }
    
    static public void showToast(String string)
    {
        Message msg = new Message();
        msg.what = TOAST;
        msg.obj = string;
        handler.sendMessage(msg);
    }
    
    static private void accessRoot()
    {
        Process process = null;
        try
        {
            process = Runtime.getRuntime().exec("su");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            process.destroy();
        }
    }

    protected void showShortToast(String string)
    {
        Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
    }

    static protected boolean isInAirplaneMode()
    {
        return android.provider.Settings.System.getInt(getApp().getContentResolver(), android.provider.Settings.System.AIRPLANE_MODE_ON, 0) == 1;
    }

    /**
     * Init app context which is essential when using Utils. Must be called before Custom App's onCreate.
     */
    @Override
    public void onCreate()
    {
        instance = this;
        super.onCreate();
        Utilities.initContext(this);
    }
    
    public static void alert(String info, Activity activity)
    {
        AlertDialog.Builder builder;
        builder = new AlertDialog.Builder(activity)
                .setTitle("提示").setMessage(info)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setPositiveButton("确定", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        dialog.dismiss();
                    }
                });
        builder.create().show();
    }
}
